home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Advanced I⁄O v2.3 / Advanced i⁄o / myenv.cc < prev    next >
Text File  |  1995-06-05  |  3KB  |  115 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *            Service C++ functions 
  5.  *         that support the standard environment for me
  6.  *
  7.  * $Id: myenv.cc,v 1.3 1995/02/08 17:23:50 oleg Exp oleg $
  8.  */
  9.  
  10. #ifdef __GNUC__
  11. #pragma implementation
  12. #endif
  13.  
  14. #include "myenv.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19.  
  20. /*
  21.  *-----------------------------------------------------------------------
  22.  *        Some global constant pertaining to input/output
  23.  */
  24.  
  25. const char _Minuses [] = "\
  26. -------------------------------------------------------------------------------";
  27.  
  28. const char _Asteriscs [] = "\
  29. *******************************************************************************";
  30.  
  31. const char _Equals [] = "\
  32. ===============================================================================";
  33.  
  34.  
  35. /*
  36.  *------------------------------------------------------------------------
  37.  *            Print an error message at stderr and abort
  38.  * Synopsis
  39.  *    volatile void _error(const char * message,... );
  40.  *    Message may contain format control sequences %x. Items to print 
  41.  *    with the control sequences are to be passed as additional arguments to
  42.  *    the function call.
  43.  */
  44.  
  45. volatile void _error(const char * message,...)
  46. {
  47.   va_list args;
  48.   va_start(args,message);        /* Init 'args' to the beginning of */
  49.                     /* the variable length list of args*/
  50.   fprintf(stderr,"\n_error:\n");     
  51.   vfprintf(stderr,message,args);
  52.   fputs("\n",stderr);
  53. #ifdef __MWERKS__
  54.   exit(4);
  55. #else  
  56.   abort();
  57. #endif
  58. }
  59.  
  60.  
  61. /*
  62.  *------------------------------------------------------------------------
  63.  *                    Print a message at stderr
  64.  * Synopsis
  65.  *    void message(const char * text,... );
  66.  *    Message may contain format control sequences %x. Items to print 
  67.  *    with the control sequences are to be passed as additional arguments to
  68.  *    the function call.
  69.  */
  70.  
  71. void message(const char * text,...)
  72. {
  73.   va_list args;
  74.   va_start(args,text);        /* Init 'args' to the beginning of */
  75.                     /* the variable length list of args*/
  76.   vfprintf(stderr,text,args);
  77. }
  78.  
  79. //------------------------------------------------------------------------
  80. //            Patches to the standard environment
  81.  
  82.                                 // Like strncpy(), but ALWAYS terminates
  83.                                 // the destination string
  84. char * xstrncpy(char * dest, const char * src, const int len)
  85. {
  86.   strncpy(dest,src,len);
  87.   dest[len] = '\0';
  88.   return dest;
  89. }
  90.  
  91. #ifndef __GNUC__
  92. #include <time.h>
  93.                                 // libg++ nifty timing functions
  94.                                 // Very rough and dirty implementation for
  95.                                 // platforms w/o libg++
  96. static time_t time_set;
  97.  
  98. double start_timer(void)
  99. {
  100.   return time_set = time(0);
  101. }
  102.                                 // return_elapsed_time(last_time) returns
  103.                                 // process time (in secs) since Last_Time
  104.                                 // If Last_time == 0.0, return time since
  105.                                 // the last call to start_timer()
  106. double return_elapsed_time(const double last_time)
  107. {
  108.   time_t new_time = time(0);
  109.   if( time_set == 0 )
  110.     return -1;                          // timer wasn't started
  111.   return new_time - (last_time == 0.0 ? time_set : last_time);
  112. }
  113.  
  114. #endif
  115.